package convertstream;

import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

/**
 * InputStreamReader:读取转换流
 *   public InputStreamReader(InputStream in, String charsetName):指定字节输入流和码表来读取文件
 *  FileReader成员方法:
 *      //JDK11以上版本才能使用
 *      public FileReader(String fileName, Charset charset):该方法的原理其实底层依然使用FileInputStream+InputStreamReader来实现
 *                                                       : new InputStreamReader(new FileInputStream(fileName), charset)
 * FileReader类原理:
 *          public FileReader(String fileName, Charset charset) throws IOException {
 *         super(new FileInputStream(fileName), charset);
 *     }
 *         public InputStreamReader(InputStream in, Charset cs) {
 *         super(in);
 *     }
 *   例如: new FileReader("day23-IO-01/GBK_04.txt")  => new InputStreamReader(new FileInputStream("day23-IO-01/GBK_04.txt"))
 *          字符读取流(FileReader) = 输入转换流(InputStreamReader) + 字节输入流(FileInputStream)
 *
 */
public class ConvertStreamDemo02 {
    public static void main(String[] args) throws Exception {
        //method01();
        //method02();
        FileReader fr = new FileReader("day23-IO-02/GBK04.txt", Charset.forName("GBK"));//直接使用FileReader指定码表
        System.out.println((char) fr.read());
        fr.close();
    }

    private static void method02() throws IOException {
        InputStreamReader isr = new InputStreamReader(new FileInputStream("day23-IO-02/GBK04.txt"), "GBK");
        System.out.println((char) isr.read());//Gbk04.txt使用的是GBK码表,读取该文件仍然使用GBK码表,因此不会出现乱码
        isr.close();
    }

    private static void method01() throws IOException {
        FileReader fr = new FileReader("day23-IO-02/GBK04.txt");
        System.out.println((char) fr.read());
        fr.close();
    }
}
